﻿using System.Linq;
using Daemon.Common;
using Daemon.Common.Helpers;
using Daemon.Infrustructure.EF;
using Daemon.Repository.Contract;
using Daemon.Model;
using System;
using System.Linq.Expressions;
using System.Collections.Generic;

namespace Daemon.Repository.EF
{
    public class ScoreImportTemplateRepository : Repository<ScoreImportTemplate>, IScoreImportTemplateRepository
    {
        protected override Expression<Func<ScoreImportTemplate, int>> PrimaryKeyExpression => r => r.Id;

        public ScoreImportTemplateRepository(ApiDBContent context) : base(context)
        {
        }

        protected override void UpdateEntitiesBeforeCreate(IEnumerable<ScoreImportTemplate> entities, ApiDBContent context)
        {
           foreach (var entity in entities)
           {
                entity.UserId = AuthInfo.UserId;
            }
        }

        protected override IQueryable<ScoreImportTemplate> FilterByAuthInfo(IQueryable<ScoreImportTemplate> queryable, ApiDBContent context)
        {
            if (AuthInfo.IsAdmin)
            {
                return queryable;
            }

            queryable = queryable.Where(r => r.UserId == AuthInfo.UserId);
            return queryable;
        }
    }
}
